package com.lsp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import com.lsp.base.empty.EmptyLayout;
import com.lsp.base.empty.EmptyLayoutDelegate;
import com.lsp.base.empty.EmptyLayoutDelegateImpl;

/**
 * 可配置状态空页面的activity基类
 *
 * 如果能使用配置的emptylayout，可通过{@getEmptyId()}配置空页面。
 *
 * 如果页面需要使用自己页面内的特殊页面。可直接通过setLoadDataError(EmptyLayout emptyLayout, View.OnClickListener
 * mOnClickListener)设置进来
 * 这时候不需要配置getEmptyId
 *
 * @author com.lsp.ibxs
 * @created 16/11/25 11:06
 */
public abstract class BaseEmptyActivity extends BaseActivity implements EmptyLayoutDelegate {

  private EmptyLayout emptyLayout;
  private EmptyLayoutDelegateImpl emptyLayoutDelegateImpl;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    emptyLayoutDelegateImpl = new EmptyLayoutDelegateImpl();
    super.onCreate(savedInstanceState);
  }

  @Override protected void initEmptyLayout() {
    super.initEmptyLayout();
    if (getEmptyId() > 0) {
      emptyLayout = (EmptyLayout) mLayoutInflater.inflate(getEmptyId(), null);
      llContentContainer.addView(emptyLayout, LinearLayout.LayoutParams.MATCH_PARENT,
          LinearLayout.LayoutParams.MATCH_PARENT);
    }
  }

  @Override
  public void setLoadDataError(EmptyLayout emptyLayoutOut, View.OnClickListener mOnClickListener) {
    if (emptyLayoutOut == null && emptyLayout == null) {
      return;
    }
    emptyLayoutDelegateImpl.setLoadDataError(emptyLayoutOut != null ? emptyLayoutOut : emptyLayout,
        mOnClickListener);
  }

  @Override public void setLoadDataSucced(EmptyLayout emptyLayoutOut) {

    emptyLayoutDelegateImpl.setLoadDataSucced(
        emptyLayoutOut != null ? emptyLayoutOut : emptyLayout);
  }

  @Override
  public void setNoData(EmptyLayout emptyLayoutOut, View.OnClickListener mOnClickListener) {
    emptyLayoutDelegateImpl.setNoData(emptyLayoutOut != null ? emptyLayoutOut : emptyLayout,
        mOnClickListener);
  }

  @Override public void setLoading(EmptyLayout emptyLayoutOut) {
    emptyLayoutDelegateImpl.setLoading(emptyLayoutOut != null ? emptyLayoutOut : emptyLayout);
  }
}
